﻿using ClassLibrary;
using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleDemo
{
    internal class LinqDemo
    {
        public static void QueryVariableTest()
        {
            int[] numbers = { 2, 5, 28 };
            IEnumerable<int> lowNums = from n in numbers
                                       where n < 20
                                       select n;
            int numsCount = (from n in numbers
                             where n < 20
                             select n).Count();
        }

        public static void JoinTest()
        {
            Student[] students = new Student[]
            {
                new Student { StID = 1, LastName = "Carson"  },
                new Student { StID = 2, LastName = "Klassen" },
                new Student { StID = 3, LastName = "Fleming"}
            };
            CourseStudent[] courseStudents = new CourseStudent[]
            {
                new CourseStudent { StID = 1, CourseName = "Art"},
                new CourseStudent { StID = 2, CourseName = "Art"},
                new CourseStudent { StID = 1, CourseName = "History"},
                new CourseStudent { StID = 3, CourseName = "History"},
                new CourseStudent { StID = 3, CourseName = "Physics"},
            };
            var query = from s in students
                        join c in courseStudents on s.StID equals c.StID
                        where c.CourseName == "History"
                        select s.LastName;
            foreach (var q in query)
            {
                Console.WriteLine(q);
            }
        }

        /// <summary>
        /// 匿名类型对象的数组 array of objects of an anonymous type
        /// </summary>
        public static void AnonymousTypeObject()
        {
            var studs = new[]
            {
                new { LName = "Jones", FName = "Mary", Age = 19 },
                new { LName = "Smith", FName = "Bob", Age = 19 },
                new { LName = "Fleming", FName = "Carol", Age = 19 },
            };
            var query = from s in studs
                        select s;
            foreach (var q in query)
            {
                Console.WriteLine($"{q.LName}, {q.FName}, {q.Age}");
            }
        }
    }
}
